package oplogd

import "gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"

const (
	// DomainID 域关唯一标识
	DomainID = "oplog"
)

// DomainOption 操作日志子域参数
type DomainOption struct {
	OpLogRepo OpLogRepo
}

// OpLogDomain 操作日志子域
type OpLogDomain interface {
	ddd.Domain
	OpLogAggregateRoot() OpLogAggregateRoot
}

// NewOpLogDomain 操作日志子域
func NewOpLogDomain(p ddd.Domain, opt DomainOption) OpLogDomain {
	d := &oplogDomain{}
	d.Init(d, p, DomainID)
	initOpLog(d, opt.OpLogRepo)
	//ddd.RegSubDomain(p, d)
	return d
}

type oplogDomain struct {
	ddd.BaseDomain
	oplogAggregateRoot OpLogAggregateRoot
}

func (i *oplogDomain) OpLogAggregateRoot() OpLogAggregateRoot {
	if i.oplogAggregateRoot != nil {
		return i.oplogAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID("oplog")
	if r == nil {
		panic("没有初始化OpLogAggregateRoot")
	}
	ir, ok := r.(OpLogAggregateRoot)
	if !ok {
		panic("初始化OpLogAggregateRoot，类型错误")
	}
	i.oplogAggregateRoot = ir
	return ir

}
